//
//  OrbitIndicator.m
//  Project 5
//
//  Created by Nguyen Tan Loc on 6/20/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "OrbitIndicator.h"

@implementation OrbitIndicator

-(id) initWithSegment:(int)segment
{
    if (self = [super init])
    {
        mSegment = segment;
        mBatchNode = [CCSpriteBatchNode batchNodeWithFile:BATCH_CIRCLE_INDICATOR_PNG];
        [self addChild:mBatchNode];
        
        CCSprite *sprite;
        for (int i = 0; i < mSegment; i++)
        {
            sprite = [CCSprite spriteWithSpriteFrameName:CIRCLE_INDICATOR_FRAME_NAME];
            [mBatchNode addChild:sprite z:0 tag:i];
            
            // cheat temp
            float ratio = 2.0 / (sprite.contentSize.width / 2);
            [sprite setScale:ratio];
        }
    }
    
    return self;
}

-(void) setRadius:(float)radius
{
    CCSprite *sprite;
    float r;
    
    for (int i = 0; i < mSegment; i++)
    {
        r = 360 * i / mSegment;
        sprite = (CCSprite*)[mBatchNode getChildByTag:i];
        [sprite setPosition:ccp(radius * sinf(CC_DEGREES_TO_RADIANS(r)), radius * cosf(CC_DEGREES_TO_RADIANS(r)))];
    }
}

-(void) runEffect:(EFFECT_TYPE)effect
{
    if (effect == EFFECT_APPEAR)
    {
        for (int i = 0; i < mBatchNode.children.count; i++)
        {
            CCSprite *sprite = (CCSprite *)[mBatchNode getChildByTag:i];
            [sprite setOpacity:0];
            
            CCAction *fadeTo = [CCFadeTo actionWithDuration:TIME_APPEAR opacity:255];
            [sprite runAction:fadeTo];
        }
    }
    else if (effect == EFFECT_DISAPPEAR)
    {
        for (int i = 0; i < mBatchNode.children.count; i++)
        {
            CCSprite *sprite = (CCSprite *)[mBatchNode getChildByTag:i];
            [sprite setOpacity:255];
            
            CCAction *fadeTo = [CCFadeTo actionWithDuration:TIME_DISAPPEAR opacity:0];
            [sprite runAction:fadeTo];
        }
    }
}

-(void) dealloc
{
    [super dealloc];
}

@end
